$(function() {
	var page = 0;
	var userInfo = "";
	//初始化函数
	function init() {
		userInfo = JSON.parse(sessionStorage.getItem("userInfo")); //获取用户登录信息
//		console.log(userInfo);
		if(userInfo){
			$('#userName').html("你好，" + userInfo.name);
			queryOrders(userInfo, page);
			setTimeout(function() {
				getimgs();
				initPaginator();
				for(var i=0; i<$('.listsItem').length; i++){
//					if($('.listsItem').eq(i).children().find('li:last-of-type button').length == 1){
//						$('.listsItem').eq(i).children().find('li:last-of-type button.seemore').css('margin-top','38px');
//						console.log("进来")
//					}
				}
			}, 1000);
		}else{
			window.location.href = "../index.html";
		}
	}
	init();

	//获取附件图片
	function getimgs(){
		for(var i=0; i<util.orderlists.length; i++){
					if(util.orderlists[i].fileIds != null && util.orderlists[i].fileIds.length>0){
						$.ajax({
							url: baseUrl + "/palmmall-server/rest/media/fileget/" + util.orderlists[i].fileIds[0],
							cache: false,
							async: false,//设置成同步
							dataType: 'json', //服务器返回json格式数据
							type: 'get', //HTTP请求类型
							contentType: "application/json",
							timeout: 20000, //超时时间设置为10秒
							success: function(res) {
//								console.log(res)
								if(res.success) {
//									console.log(i)
									$('.goodsimg').eq(i).attr('src',res.data);
								} else {
									layer.msg(res.message)
								}
							},
							error: function(res) {
								console.log(res);
								layer.msg("网络出错……");
							}
						})
					}
				}
	}
	//初始化分页标签函数
	function initPaginator() {
		pageCount = util.pageCount;
		var options = {
			bootstrapMajorVersion: 3, // bootstrap.css文件版本
			currentPage: 1, // 当前页数
			totalPages: pageCount, // 总页数
			numberOfPages: 10, //当前显示页数
			alignment: "center", //居中显示
			size: "normal",
			itemTexts: function(type, page, current) {
				switch(type) {
					case "first":
						return "首页";
					case "prev":
						return "上一页";
					case "next":
						return "下一页";
					case "last":
						return "末页";
					case "page":
						return page;
				}
			},
			//控制分页标签的显示隐藏
			shouldShowPage: function(type, page, current) {
				switch(type) {
					case "first":
						return false;
					case "last":
						return false;
					default:
						return true;
				}
			},
			// 点击事件，用于通过ajax来刷新整个list列表
			onPageClicked: function(event, originalEvent, type, page) {
				// 按分页从后台获取第 + page + 页的具体数据
				queryOrders(userInfo, page - 1);
				$(window).scrollTop(0);
				setTimeout(function(){
					getimgs();
				},1000);
			}
		};
		$('#page').bootstrapPaginator(options);
	}

	//显示隐藏更多搜索
	$('.moreSearchTxt').click(function() {
		if($('.moreSearchMain').css('display') == 'none') {
			$('.moreSearchMain').fadeIn();
		} else {
			$('.moreSearchMain').fadeOut();
		}
	})

	//点击订单查看订单详情
	$('#ordersListsMain').on('click', 'li .seeDetails', function() {
		var index = $(this).parent().parent().parent().index();
		var uuid = util.orderlists[index].uuid;
		getOrderDetails(uuid);
	});

	//点击作废按钮事件
	$('#ordersListsMain').on('click', 'li .cancelledOrder', function() {
		var index = $(this).parent().parent().parent().index();
		var uuid = util.orderlists[index].uuid;
		var version = util.orderlists[index].version;
		var time = new Date().Format("yyyy-MM-dd hh:mm:ss");
		var operId = userInfo.code;
		var operName = userInfo.name;
		layer.confirm('你确定要作废该订单？', {
			btn: ['确定', '取消'] //按钮
		}, function() {
			cancelledOrder(uuid, version, time, operId, operName);
			layer.closeAll('dialog');
		});

	});
})